package com.ibp.service.impl;

import java.util.Map;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ibp.form.CmdForm;
import com.ibp.other.EventLogReader;
import com.ibp.service.DictDataService;
import com.ibp.service.ExecuteCmdService;
import com.ibp.service.FSHttpService;
import com.ibp.utils.SocketClientUtils;

@Service("executeCmdServiceImpl")
public class ExecuteCmdServiceImpl implements ExecuteCmdService,InitializingBean {
	private static Logger logger = LoggerFactory.getLogger(ExecuteCmdServiceImpl.class);

	private String ibmsCmd_ip = null;
	private String ibmsCmd_port = null;

	private String iblogCmd_ip = null;
	private String iblogCmd_port = null;

	
	
	private ServletContext servletContext;

	@Autowired
	DictDataService dictDataServiceImpl;
	
	
	@Override
	public boolean executeIbmsCmd(String cmd) {
		return SocketClientUtils.sendexecuteCmd(this.ibmsCmd_ip, this.ibmsCmd_port, cmd);
	}

	@Override
	public String executeIbmsCmdData(String cmd) {
		return SocketClientUtils.sendDataCmd(this.ibmsCmd_ip, this.ibmsCmd_port, cmd);
	}

	@Override
	public boolean executeIblogCmd(String cmd) {
		return SocketClientUtils.sendexecuteCmd(this.iblogCmd_ip, this.iblogCmd_port, cmd);
	}

	@Override
	public String executeIblogCmdData(String cmd) {
		return SocketClientUtils.sendDataCmd(this.iblogCmd_ip, this.iblogCmd_port, cmd);
	}

	@Override
	public boolean executeIbcmd(CmdForm cmdForm) {
		  String ip = cmdForm.getIp();
		  String port = cmdForm.getPort();
		  String cmd = cmdForm.getCmd();
		  return SocketClientUtils.sendexecuteCmd(ip, port, cmd);
	}

	@Override
	public String executeIbcmdData(CmdForm cmdForm) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void afterPropertiesSet() throws Exception {
	  	this.servletContext = dictDataServiceImpl.getServletContext();
	  	logger.info("初始化ExecuteCmdServiceImpl服务节点ip和port配置,servletContext:"+servletContext); 
	    Map<String,Map<String,String>> dictMap = (Map<String, Map<String, String>>) this.servletContext.getAttribute("dictMap");
	    
	    this.ibmsCmd_ip =    dictMap.get("tcp.ibms").get("ip");
	    this.ibmsCmd_port =  dictMap.get("tcp.ibms").get("port");
	    this.iblogCmd_ip = 	 dictMap.get("tcp.iblog").get("ip");
		this.iblogCmd_port = dictMap.get("tcp.iblog").get("port") ;
		
	}


}
